﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ServBus.ListForm2010.Common.XSNOperator;
using System.Collections.Generic;

namespace ServBus.ListForm2010.Layouts.InfoBus2010
{
    public partial class Template : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    TestGenerateXSN();
                }
            }
            catch (Exception ex)
            {
                ExLit.Text = ex.ToString();
            }
        }

        private void TestGenerateXSN()
        {
            Guid _siteId = SPContext.Current.Site.ID;
            Guid _webId = SPContext.Current.Web.ID;
            using (SPSite _site = new SPSite(_siteId))
            {
                using (SPWeb _web = _site.OpenWeb(_webId))
                {
                    SPList _list = _web.Lists["test"];
                    SPContentType _CType = _list.ContentTypes["TestItem3"];
                    //Main List
                    XListInfo _mListInfo = new XListInfo();
                    _mListInfo.GenerateListInfo(_list);
                    _mListInfo.AddContentTypeInfo(_CType);

                    //Sub List
                    SPList _slist1 = _web.Lists["ListForm Test"];
                    SPContentType _sCType11 = _slist1.ContentTypes["Item"];
                    SPContentType _sCType12 = _slist1.ContentTypes["TestItem"];
                    XListInfo _sListInfo1 = new XListInfo();
                    _sListInfo1.GenerateListInfo(_slist1);
                    _sListInfo1.AddContentTypeInfo(_sCType11);
                    _sListInfo1.AddContentTypeInfo(_sCType12);

                    SPList _slist2 = _web.Lists["Customer"];
                    SPContentType _sCType21 = _slist2.ContentTypes["Item"];
                    XListInfo _sListInfo2 = new XListInfo();
                    _sListInfo2.GenerateListInfo(_slist2);
                    _sListInfo2.AddContentTypeInfo(_sCType21);

                    List<XListInfo> _SListInfos = new List<XListInfo>();
                    _SListInfos.Add(_sListInfo1);
                    _SListInfos.Add(_sListInfo2);

                    #region XSNOperator Initial

                    XSNOperator _xsnOperator = new XSNOperator();
                    _xsnOperator.SiteId = _siteId;
                    _xsnOperator.WebId = _webId;
                    _xsnOperator.ListId = _list.ID;
                    _xsnOperator.CTypeId = _CType.Id;
                    _xsnOperator.XSNTemplateFolder = Server.MapPath("/_layouts/InfoBus2010/xsn_template/");
                    _xsnOperator.G_Site_Url = _web.Url;
                    _xsnOperator.G_XSN_Url = _web.Url + _list.RootFolder.ServerRelativeUrl + "/" + _CType.Name + "/template.xsn";

                    _xsnOperator.MListInfo = _mListInfo;
                    _xsnOperator.SListsInfo = _SListInfos;

                    _xsnOperator.GenerateXSN();
                    #endregion
                }
            }
        }
    }
}
